use std::{env, error::Error, fs::File};

use log::LevelFilter;
use simplelog::{CombinedLogger, ConfigBuilder, SimpleLogger, WriteLogger, format_description};
use time::UtcOffset;

pub fn init_logger() -> Result<(), Box<dyn Error>> {
    let config = ConfigBuilder::default()
        .set_time_format_custom(format_description!(
            "[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"
        ))
        .set_time_offset(UtcOffset::from_hms(8, 0, 0)?)
        .build();
    let level_filter: LevelFilter = env::var("RUST_LOG")
        .unwrap_or_else(|_| "info".to_string())
        .parse()?;

    CombinedLogger::init(vec![
        WriteLogger::new(level_filter, config.clone(), File::create("app.log")?),
        SimpleLogger::new(level_filter, config),
    ])?;
    Ok(())
}
